<?php
namespace Sys\Controller;

use Sys\Model\AdminMenuModel;
use Think\Controller;
use Common\Controller\UBaseController;

class MenuController extends UBaseController
{
    public function index()
    {
        $menuDao = new AdminMenuModel();
        $menuList = $menuDao->getForManager();
        $this->assign("list", $menuList);
        //设置页面标题,非必须
        $this->_title("目录配置");
        //页面ID
        $this->assign("pageId", "A002002");
        //页面公共模板调用
        layout("default");
        //选取数据模板
        $this->display('index');
    }

    public function power()
    {
        //设置页面标题,非必须
        $this->_title("目录配置");
        //页面ID
        $this->assign("pageId", "A002002");
        //页面公共模板调用
        layout("default");
        //选取数据模板
        $this->display('power');
    }

    public function addp()
    {
        $menuModel = new AdminMenuModel();

        $level2Count = $menuModel->where(['level'=>2])->count();

        $next = $level2Count+1;
        if ($next >= 10) {
            $nextLevel2 = 'A0' . $next;
        } else {
            $nextLevel2 = 'A00' . $next;
        }

        $level2List = $menuModel->where(['level'=>2, 'status'=>1])->select();

        foreach($level2List as $k => $lv2) {
            $sonCount = $menuModel->where(['level'=>3, 'parent_id'=>$lv2['id']])->count();

            $nextSonId = $sonCount+1;
            if ($nextSonId >= 10) {
                $sonLevel = $lv2['level_sign'] . '0' . $nextSonId;
            } else {
                $sonLevel = $lv2['level_sign'] . '00' . $nextSonId;
            }

            $level2List[$k]['next_son'] = $sonLevel;
        }

        $this->assign('level2List', $level2List);
        $this->assign('nextLevel2', $nextLevel2);
        //设置页面标题,非必须
        $this->_title("目录配置");
        //页面ID
        $this->assign("pageId", "A002001");
        //页面公共模板调用
        layout("default");
        //选取数据模板
        $this->display('add');
    }

    public function ajax()
    {
        $action = $this->params['action'];
        if (empty($action)) {
            $this->jsonReturn(10000,"","参数不全");
        }

        $now = time();
        switch ($action) {
            case 'addMenu' :
                //新增目录
                if (empty($this->params['menu_title'])
                    || empty($this->params['level_sign'])
                    || empty($this->params['menu_level'])
                ) {
                    $this->jsonReturn(10000,"","参数不全");
                }

                $menuDao = new AdminMenuModel();
                $sonCount = $menuDao->getSonCountByParentId($this->params['parent_id']);

                $newMenu = [
                    'title'         => $this->params['menu_title'],
                    'parent_id'     => empty($this->params['parent_id']) ? 0 : $this->params['parent_id'],
                    'level_sign'    => $this->params['level_sign'],
                    'uri'           => empty($this->params['menu_uri']) ? NULL : $this->params['menu_uri'],
                    'level'         => $this->params['menu_level'],
                    'order_index'   => $sonCount + 1,
                    'created'       => $now,
                    'status'        => 1,
                    'type'          => $this->params['menu_level'] == 3 ? 2 : 1
                ];

                $res = $menuDao->add($newMenu);
                if ($res) {
                    $this->jsonReturn(200, "", "新增成功");
                } else {
                    $this->jsonReturn(10001, "", "新增失败");
                }
                break;
            case 'deleteMenu' :
                //删除目录
                break;
            case 'orderMenu' :
                //目录排序
                if (empty($this->params['menu_id'])
                    || empty($this->params['move'])
                    || empty($this->params['parent_id'])) {
                    $this->jsonReturn(10000,"","参数不全");
                }
                $menuDao = new AdminMenuModel();
                $menuDao->rebuildOrder(intval($this->params['parent_id']), $this->params['menu_id'], $this->params['move']);
                $this->jsonReturn(200, "", "操作成功");
                break;
            default:
                $this->jsonReturn(10000, "", "参数不全");
                break;
        }

        exit;
    }
}